var b,c;
require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var vm = new Vue({
    el:"#selfMediaVd",
    data:{
        commentTab:2,
        loadFlag:false,
        isInfoShow:false,
        isGoods:false,
        isComment:false,//评论显示隐藏
        id:"",//视频id
        uid:"",//用户id
        sourceType:"",
        praiseState:"",//点赞状态
        praiseCount:"",//点赞人数
        collectionState:"",//收藏状态
        collectionCount:"",//收藏总数
        mediaAdvAdvById:{},//视频信息
        pageNumber:1,
        commentList:{},
        commentList1:[],
        commentList2:{},
        rewardCount:"",//已打赏金额
        isReward:false,
        isRewardNum:1,
        moneyList:[{value:"50"},{value:"100"},{value:"200"},{value:"200"},{value:"1000"},{value:"2000"}],
        rewardMoney:"",
        money:"", //打赏金额
        commentText:"",
        release:{},
        isScreen:"",
        totalTime:"",
        num:"",  //倒计时显示
        money1:"",  //抢到的金额
        isShare:false,
        titleLength:"",
        vhenght:"",
        whereYouAt:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.id=this.searchObject&&this.searchObject.id;
        this.uid=this.searchObject&&this.searchObject.uid;
        this.sourceType=this.searchObject&&this.searchObject.sourceType;
        this.releaseConfig();
        this.vhenght=getWindowHeight();
        // $("#selfMediaVd").height(this.vhenght);
        this.getInfoMediaAdvCommentList();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"/imgs/see/index/index-bgimg1.png";
            return {
                "background-image":'url("'+url+'"),url("/imgs/see/index/index-bgimg1.png")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //显示
        getInfoShow:function(){
            this.isInfoShow=!this.isInfoShow;
        },
        //获取播放视频信息
        getMediaAdvAdvById:function(){
            var that=this;
            if(that.mediaAdvAdvById&&that.mediaAdvAdvById.imgSrc){
                player.dispose();
                $(".selfMediaVd-video").append("<div id='my-video'></div>");
            }
            var data={
                id:that.id,
                loginUid:that.uid,
                sourceType:that.sourceType
            };
            console.log(data)
            this.ajax({url:"/selfMedia/getMediaAdvAdvById",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.mediaAdvAdvById=b.json.data;
                    that.praiseState=that.mediaAdvAdvById.praiseState;
                    that.praiseCount=that.mediaAdvAdvById.praiseCount;
                    that.collectionState=that.mediaAdvAdvById.collectionState;
                    that.collectionCount=that.mediaAdvAdvById.collectionCount;
                    that.rewardCount=that.mediaAdvAdvById.rewardCount;
                    that.dataLength(that.mediaAdvAdvById.title);
                    // that.vid=that.mediaAdvAdvById.imgSrc;
                    //console.log(that.mediaAdvAdvById);
                   /* if(that.mediaAdvAdvById){
                        return false;
                    }else{
                        getPlayer(that.mediaAdvAdvById.imgSrc);
                    }*/
                    getPlayer(that.mediaAdvAdvById.imgSrc);
                    that.getHistoryAdd();
                    //player.cancelFullScreen();
                    //player.play();
                    //getCancelFullScreen();
                    // if(that.mediaAdvAdvById.rewardCount>0&&that.mediaAdvAdvById.historyId!=null){
                    //     // var totalTime=player.getDuration();
                    //     // console.log(totalTime)
                    //     that.getCanvas();
                    // }
                    // getPlayer(that.mediaAdvAdvById);
                    // that.getVideo(that.mediaAdvAdvById.imgSrc)
                    //http://vod.cn-shanghai.aliyuncs.com/?Action=GetVideoPlayAuth&VideoId=93ab850b4f6f44eab54b6e91d24d81d4&Format=JSON
                    // myPlayer.src(that.mediaAdvAdvById.imgSrc);
                    // myPlayer.load();
                    // myPlayer.play();
                    // console.log(that.praiseState)
                    // console.log(that.collectionState)
                }else{
                    vm.$refs.prompt.toast(b.json.msg||"获取视频信息失败！");
                }
            });
        },
        getCanvas:function(){
            var that=this;
            b=setInterval(function(){
                var totalTime=player.getDuration();
                // alert(totalTime)
                var whereYouAt = player.getCurrentTime();
                if(whereYouAt>that.whereYouAt+2){
                    player.seek(that.whereYouAt+1);
                    return false;
                }
                that.whereYouAt=whereYouAt;
                // alert(that.whereYouAt)
                that.totalTime=Math.floor(totalTime)-Math.floor(whereYouAt);
                console.log(vm.totalTime)
                if(that.totalTime==1){
                    that.totalTime=0;
                    clearInterval(b);
                    // that.num=2;
                    that.getIsScreen(1);
                }else if(that.totalTime<=30){
                    that.num=1;
                }
            },1000);
        },
        //增加播放历史 返回奖励信息
        getHistoryAdd:function(){
            var that=this,data;
            data={
                loginUid:that.uid,
                advId:that.id
            };
            this.ajax({url:"/happyi/user/historyAdd",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){

                }else{
                    vm.$refs.prompt.toast("增加播放历史失败！");
                }
            });
        },
        //打开红包
        getRedPacketsMediaAdvById:function(){
            var that=this,data;
            data={
                loginUid:that.uid,
                id:that.id
            };
            console.log(data)
            this.ajax({url:"/selfMedia/getRedPacketsMediaAdvById",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.money1=b.json.data.money;
                    if(that.money1==0){
                        that.num=2;
                        that.getIsScreen(3);
                    }else{
                        that.getIsScreen(2);
                    }
                    that.getMediaAdvAdvById();
                    // that.money1=b.json.data.money;
                }else{
                    that.num=2;
                    that.getIsScreen(3);
                    //vm.$refs.prompt.toast("打开红包失败！");
                }
            });
        },
        //获取配置文件
        releaseConfig:function(){
            var that=this,data;
            data={
                loginUid:that.uid
            };
            console.log(data)
            this.ajax({url:"/release/releaseConfig",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.release=b.json.data;
                    console.log(that.release)
                    that.getMediaAdvAdvById();
                }else{
                    if(b.json.msg="用户未登录"){
                        location.href="/telLogin?type=1";
                    }else{
                        vm.$refs.prompt.toast(b.json.msg||"获取视频信息失败！");
                    }
                }
            });
        },
        //视频暂停
        getVideoPause:function(){
            // console.log(this.isInfoShow)
            if(this.isInfoShow){
                // var whereYouAt = myPlayer.currentTime();
                // myPlayer.currentTime(whereYouAt);
                player.play();
                if(this.mediaAdvAdvById.isRed==1&&!this.mediaAdvAdvById.historyId){
                    this.getCanvas();
                }
                // this.getCanvas();
            }else{
                player.pause();
                clearInterval(b);
                // myPlayer.pause();
            }
            this.isInfoShow=!this.isInfoShow;
            // myPlayer.pause();
        },
        //通过用户id 视频id state点赞状态 1点赞0取消点赞
        getPraise:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                id:that.id
            };
            if(that.praiseState==0){
                data.state=1;
            }else{
                data.state=0;
            }
            //console.log(data);
            this.ajax({url:"/selfMedia/getPraise",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getMediaAdvAdvById();
                   /* that.praiseCount=b.json.data;
                    if(that.praiseState==0){
                        that.praiseState=1;
                    }else{
                        that.praiseState=0;
                    }*/
                    // console.log(that.praiseCount)
                }else{
                    vm.$refs.prompt.toast("点赞失败！")
                }
            });
        },
        //通过用户id 视频id state收藏状态 1收藏0取消收藏
        getCollection:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                id:that.id
            };
            if(that.collectionState==0){
                data.state=1;
            }else{
                data.state=0;
            }
            //console.log(data)
            this.ajax({url:"/selfMedia/getCollection",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getMediaAdvAdvById();
                   /* that.collectionCount=b.json.data;
                    if(that.collectionState==0){
                        that.collectionState=1;
                    }else{
                        that.collectionState=0;
                    }*/
                    //console.log(that.collectionState)
                }else{
                    vm.$refs.prompt.toast("收藏失败！");
                }
            });
        },
        //显示评论
        getComment:function(type){
            if(!this.isReward){
                this.commentTab=type;
                if(type==2){
                    this.getInfoMediaAdvCommentList();
                }
                this.isComment=!this.isComment;
            }
        },
        //tab切换
        getCommentTab:function(num){
            this.commentTab=num;
            if(num==2){
                this.getInfoMediaAdvCommentList();
            }
        },
        getGoods:function(){
            if(!this.isReward){
                this.isGoods=!this.isGoods;
            }
        },
        //时间
        getData:function(data){
            return data.split(' ')[0];
        },
        //获取自媒体评论列表
        getInfoMediaAdvCommentList:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                id:that.id,
                pageNumber:that.pageNumber
            };
            //console.log(data)
            this.ajax({url:"/selfMedia/getInfoMediaAdvCommentList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==00&&b.json.data){
                    that.commentList=b.json.data;
                    that.commentList1=that.commentList.list;
                    if(that.commentList.pageNumber>1){
                        if(that.commentList.list.length>0) {
                            that.commentList2 = that.commentList2.concat(b.json.data.list);
                        }
                    }else{
                        that.commentList2= that.commentList1;
                    }
                    vm.loadFlag=false;
                    console.log(that.commentList)
                }else{
                    that.commentList={};
                    that.commentList1=[];
                    that.commentList2=[];
                    vm.$refs.prompt.toast("获取评论列表！");
                }
            });
        },
        getReward:function(num){
            // this.isRewardNum=1;
            this.isReward=num;
        },
        getIsRewardNum:function(num){
            // this.rewardMoney="";
            this.isRewardNum=num;
        },
        getIsScreen:function(num) {
            this.isScreen=num;
        },
        //自媒体打赏
        mediaReward:function(){
            var that=this;
            console.log()
            /*if(money){that.money=money;}
            console.log(that.money)
            if(!that.money){
                vm.$refs.prompt.toast("打赏金额为空，不能打赏！");
                return false;
            }else if(that.money<1){
                that.rewardMoney="";
                vm.$refs.prompt.toast("打赏金额最小为1乐币！");
                return false;
            }*/
            var data={
                id:that.id,
                loginUid:that.uid,
                money:1
            };
            console.log(data)
            this.ajax({url:"/selfMedia/mediaReward",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    // that.mediaRewardInfo=b.json.data;
                    that.rewardCount=b.json.data;
                    that.getReward(1);
                    c=setInterval(function(){
                        that.getReward();
                    },3000);
                    // vm.$refs.prompt.toast("打赏成功！");
                    // that.getReward();
                    console.log(b.json.data)
                }else{
                    if(b.json.msg=="金币不够"){
                        that.getReward(2);
                        c=setInterval(function(){
                            that.getReward();
                        },3000);
                    }else{
                        vm.$refs.prompt.toast("打赏失败！");
                    }
                }
            });
        },
        //评论点赞
        praiseMediaAdvComment:function(state,commentId){
            var that=this;
            console.log(state+"----"+commentId)
            var data={
                loginUid:that.uid,
                commentId :commentId
            };
            if(state==0){
                data.state=1;
            }else{
                data.state=0;
            }
            console.log(data)
            this.ajax({url:"/selfMedia/praiseMediaAdvComment",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                   that.commentList2.forEach(function(val){
                       if(val.commentId==commentId){
                           val.praiseState=data.state;
                           val.praiseCount=b.json.data.praiseCount;
                       }
                   });
                    console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("点赞失败！");
                }
            });
        },
        //发表评论
        comment:function(){
            var that=this;
            if(!that.commentText){
                vm.$refs.prompt.toast("请输入评论内容！");
                return false;
            }
            var data={
                loginUid:that.uid,
                content:that.commentText,
                id:that.id
            };
            console.log(data);
            this.ajax({url:"/selfMedia/comment",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.commentText="";
                    that.getInfoMediaAdvCommentList();
                    that.getMediaAdvAdvById();
                    //console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("评论失败！");
                }
            });
        },
        getCommentInfo:function(commentId){
            location.href="/see/commentInfo?id="+this.id+"&loginUid="+this.uid+"&commentId="+commentId;
        },
        //跳转页面
        getNext:function(recruitId){
            location.href="/see/jobDetails?recruitId="+recruitId;
        },
        //跳转
        getUserInfo:function(num,item){
            var path,that=this;
            console.log(item)
            if(num==1){   //个人商品详情
                path="/userGoodsInfo?productId="+item.productId;
            }
            location.href=path;
        },
        getShare:function(){
            this.isShare=!this.isShare;
            if(!this.isShare&&this.totalTime!=0){
                player.play();
            }else{
                player.pause();
            }
        },
        getChoice:function(num){
            if(num==1){
                location.href="/friend/choiceList?number=2&postId="+this.id+"&sourceType="+this.sourceType;
            }else{
                location.href="/friend/forward?number=2&postId="+this.id+"&sourceType="+this.sourceType;
            }
        },
        dataLength:function(fData){
            var intLength=0
            for (var i=0;i<fData.length;i++){
                if ((fData.charCodeAt(i) < 0) || (fData.charCodeAt(i) > 255))
                    intLength=intLength+2
                else
                    intLength=intLength+1
            }
            this.titleLength=intLength;
            //console.log(intLength)
            // return intLength
        }
    }
    ,watch:{
        commentTab:function(){
            if(this.commentTab==3){
                this.$nextTick(function(){
                    var swiper1 = new Swiper(".signMarriage-album .swiper-container",{
                        spaceBetween: 5,
                        slidesPerView: 'auto',
                        freeMode: true,
                        lazy: {
                            loadPrevNext: true,
                            loadPrevNextAmount: 5,
                            loadOnTransitionStart: true
                        },
                        observer: true,//修改swiper自己或子元素时，自动初始化swiper
                        observeParents: true//修改swiper的父元素时，自动初始化swiper
                    });
                });
            }
        },
        isGoods:function(){
            if(this.isGoods){
                this.$nextTick(function(){
                    var swiper1 = new Swiper(".selfMediaVd-goodsCent .swiper-container",{
                        spaceBetween: 5,
                        slidesPerView: 'auto',
                        freeMode: true,
                        lazy: {
                            loadPrevNext: true,
                            loadPrevNextAmount: 5,
                            loadOnTransitionStart: true
                        },
                        observer: true,//修改swiper自己或子元素时，自动初始化swiper
                        observeParents: true//修改swiper的父元素时，自动初始化swiper
                    });
                });
            }
        },
        commentList2:function(){
            this.$nextTick(function(){
                console.log($(".comment-cent").height());
                getScroll();
            });

        },
        rewardMoney:function(){
            this.money=Number(this.rewardMoney);
        }
    }

});
var player;
function getPlayer(imgSrc){
    player = new Aliplayer({
        id: "my-video",
        "width":"100%",
        "height":"100%",
        //播放方式四：使用STS方式播放
        "vid": imgSrc,//"d388b80a857f4333a39177a5df8005db",//imgSrc,
        "accessKeyId": vm.release.accessKeyId,
        "securityToken": vm.release.securityToken,
        "accessKeySecret": vm.release.accessKeySecret,
        "autoplay": false,
        "isLive": false,
        "rePlay": false,
        "playsinline": true,
        "preload": true,
        "controlBarVisibility": "click",
        "showBarTime": 0,
        "useH5Prism": true,
        // "showBuffer":false,
        "x5_type":'h5', //通过 video 属性 “x5-video-player-type” 声明启用同层H5播放器，支持的值：h5 https://x5.tencent.com/tbs/guide/video.html
        "x5_fullscreen":false//通过 video 属性 “x5-video-player-fullscreen” 声明视频播放时是否进入到 TBS 的全屏模式，支持的值：true
        // "x5_video_position":"top",
        // "x5_orientation":"portraint"
        // "skinLayout": [
        //     {
        //         "name": "controlBar",
        //         "align": "blabs",
        //         "x": 0,
        //         "y": 0,
        //         "children": [
        //             {
        //                 "name": "progress",
        //                 "align": "blabs",
        //                 "x": 84,
        //                 "y": 100
        //             },
        //             {
        //                 "name": "playButton",
        //                 "align": "tl",
        //                 "x": 13,
        //                 "y": -66
        //             },
        //             {
        //                 "name": "timeDisplay",
        //                 "align": "tl",
        //                 "x": 42,
        //                 "y": -76
        //             }
        //         ]
        //     }
        // ]
    },function(player){
        console.log('播放器创建好了。');
        $("#my-video video").attr("poster",vm.mediaAdvAdvById.coverImgSrc);
        //
    });
    //player.seek();
    // player.on("seek", function(){
    //     alert("dddd")
    //     // setLayout();
    // });
    // player.exitFullScreen();
    // player.ended(function(){
    //     console.log("dddddddddddddddddd")
    // });
}
function getCancelFullScreen(){
    //player.cancelFullScreen();
    //window._firstFullscreen = true;
    // player.on("requestFullScreen",function(){
    //     if(window._firstFullscreen){
    //         alert("aaaaa")
    //         player.cancelFullScreen();
    //         window._firstFullscreen = false;
    //     } else {
    //        $("#my-video").find("video").addClass("center");
    //        alert("ddddddddd")
    //     }
    // });
}
// var setLayout = function() {
//     //设置播放器容器的高度
//     var height ; //根据实际情况设置高度
//     player.el().style.height = vm.vhenght;
// };
// window.onresize = function(){
//     setLayout();
// };
// player.on("requestFullScreen", function(){
//     setLayout();
// });
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
// var myPlayer = videojs('my-video',{
//     loop:true,
//     controlBar: {
//         //fullscreenToggle:false,
//         volumeMenuButton:false
//     }
// });
function getScroll(){
    var nScrollHight = 0; //滚动距离总长(注意不是滚动条的长度)
    var nScrollTop = 0;   //滚动到的当前位置
    var nDivHight = $(".comment-cent").height();
    console.log(nDivHight)
    $(".comment-cent").scroll(function(){
        var loadFlag = vm.commentList.lastPage;
        //监听事件内容
        nScrollHight = $(this)[0].scrollHeight;
        nScrollTop = $(this)[0].scrollTop;
        if(nScrollTop + nDivHight >= nScrollHight){
            if(!loadFlag){
                vm.loadFlag=true;
                vm.pageNumber++;
                vm.getInfoMediaAdvCommentList();
            }else{
                vm.$refs.prompt.toast("没有更多了！")
            }
        }
    });
}